package com.example.musicapp.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.MediaStore;

import androidx.core.app.ActivityCompat;

import com.example.musicapp.application.MyApp;
import com.example.musicapp.bean.Music;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {

    /**
     * 判断是否为手机号码
     * @param mobiles 手机号
     * @return 返回对错
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * md5 工具类
     * @param url  你要转化的东东
     * @return 转化后的MD5
     */
    public static String hashKeyFormUrl(String url) {
        String cacheKey;
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(url.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(url.hashCode());
        }
        return cacheKey;
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * 扫描本地音乐的方法
     * @return
     */
    public static ArrayList<Music> getAllMusic( ){
        ArrayList<Music> data=new ArrayList<>();
        Cursor cursor = MyApp.getContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                null, MediaStore.Audio.Media.IS_MUSIC);//第一个参数为某个应用程序的某一张表，2为查询的列名，3为约束条件，4为占位符号，5为查询结果的排序方式
        if (cursor!=null) {
            while (cursor.moveToNext()){
                Music music=new Music();
                music.setSong(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                music.setSinger(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                music.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                music.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                music.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));//歌曲长度
                music.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));//歌曲大小
                if(music.getSize()>1000 * 800){
                    //进行分割获取对应格式
                    if (music.getSong().contains("-")) {
                        String[] str = music.getSong().split("-");
                        music.setSinger(str[0]);
                        music.setSong(str[1]);
                    }
                    data.add(music);
                }

            }
        }
        return data;
    }

    /**
     * 定义一个方法用来格式化获取到的时间
     */
    public static String formatTime(int time) {
        if (time / 1000 % 60 < 10) {
            return time / 1000 / 60 + ":0" + time / 1000 % 60;
        } else {
            return time / 1000 / 60 + ":" + time / 1000 % 60;
        }
    }

    public static int judgeNum(int position, List list){

        if(position >= 0 && position < list.size()){
            return position;
        }

        while (position<0 ){
            position+=list.size();
            position = judgeNum(position,list);
            if (position >= 0 && position < list.size()){
                return position;
            }
        }
        while (position>=list.size()){
            position = position%list.size();
            if (position >= 0 && position < list.size()){
                return position;
            }
        }
        return 0;
    }


}
